//
//  AddresssViewController.m
//  EDPS
//
//  Created by dllo on 15/12/7.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "AddresssViewController.h"

@interface AddresssViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,retain) NSMutableArray *provinceArray;
@property (nonatomic,retain) NSMutableArray *bigArray;
@property (nonatomic,retain) NSString *city;

@end

@implementation AddresssViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.provinceArray = [NSMutableArray array];
    self.bigArray = [NSMutableArray array];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    [self creatViews];
    [self creatData];
}
- (void)creatData
{
    NSString *pathFile = [[NSBundle mainBundle] pathForResource:@"address" ofType:@"plist"];
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:pathFile];
    self.bigArray = [dictionary objectForKey:@"address"];
    for (NSMutableDictionary *dic in self.bigArray)
    {
        NSString *province = [dic objectForKey:@"name"];
        //省  数组provinceArray
        [self.provinceArray addObject:province];
    }
}
- (void)creatViews
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.provinceArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableDictionary *dic = [self.bigArray objectAtIndex:section];
    NSMutableArray *cityarray = [dic objectForKey: @"sub"];
    return cityarray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *deq = @"deq";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:deq];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:deq];
    }
    NSMutableDictionary *dic = [self.bigArray objectAtIndex:indexPath.section];
    NSMutableArray *cityarray = [dic objectForKey: @"sub"];
    NSMutableDictionary *dict = [cityarray objectAtIndex:indexPath.row];
    cell.textLabel.text = [dict objectForKey:@"name"];
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.provinceArray objectAtIndex:section];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *dic = [self.bigArray objectAtIndex:indexPath.section];
    NSMutableArray *cityarray = [dic objectForKey: @"sub"];
    NSMutableDictionary *dict = [cityarray objectAtIndex:indexPath.row];
    self.city = [dict objectForKey:@"name"];
    [self.delegate passDelegate:self.city];
    [self back];
}
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
